<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Request;
use think\facade\Filesystem;

class Product extends Controller
{
    protected $middleware = ['Auth'];

    public function add(){
        $post = input('post.');
        $standardFile = request()->file('standard_file');
        $sourceFile = request()->file('source_file');
        $standardFileInfo = $this->saveFile($standardFile);
        $sourceFileInfo = $this->saveFile($sourceFile);
        $post["standard_file_url"] = $standardFileInfo->getSaveName();
        $post["source_file_url"] = $sourceFileInfo->getSaveName();
        $post["standard_file_name"] = $standardFile->getInfo()['name'];
        $post["source_file_name"] = $sourceFile->getInfo()['name'];
        try{
            if(Db::name("product")->insert($post))
                return json(["code"=>200,"msg"=>"添加成功"]);
            return json(["code"=>-1,"msg"=>"添加失败"]);
        } catch (\Exception $e) {
            if ($e->getData()["PDO Error Info"]["Driver Error Code"] == 1062)
                return json(["code"=>-1,"msg"=>"该产品已存在"]);
            throw $e->getMessage();
        };
    }

    public function edit(){
        $post = input('post.');
        $standardFile = request()->file('standard_file');
        $sourceFile = request()->file('source_file');
        $standardFileInfo = $this->saveFile($standardFile);
        $sourceFileInfo = $this->saveFile($sourceFile);
        $post["standard_file_url"] = $standardFileInfo->getSaveName();
        $post["source_file_url"] = $sourceFileInfo->getSaveName();
        $post["standard_file_name"] = $standardFile->getInfo()['name'];
        $post["source_file_name"] = $sourceFile->getInfo()['name'];
        $post['status'] = 0;
        $id = $post['id'];
        try{
            if(Db::name("product")->where(['id'=>$id])->update($post))
                return json(["code"=>200,"msg"=>"修改成功"]);
            return json(["code"=>-1,"msg"=>"修改失败"]);
        } catch (\Exception $e) {
            if ($e->getData()["PDO Error Info"]["Driver Error Code"] == 1062)
                return json(["code"=>-1,"msg"=>"该产品已存在"]);
            throw $e->getMessage();
        };
    }

    public function approval(){
        $post = input('post.');
        $data['status'] = $post['type'];
        if($post['approvalRefuseMsg'] != '')
            $data['approval_refuse_msg'] = $post['approvalRefuseMsg'];
        if(Db::name('product')->where(['id'=>$post['id'],'status'=>0])->update($data))
            return json(["code"=>200,"msg"=>"处理成功"]);
        return json(["code"=>-1,"msg"=>"处理失败"]);
    }

    private function saveFile($file){
        $info = $file->rule('date')->move( '../uploads/');
        if(!$file)
            return json(['code'=>0,'msg'=>'文件上传失败','data'=>['error'=>$file->getError()]]);
        return $info;
    }

    public function delete(){
        $post = input('post.');
        if(!$product = Db::name('product')->where(['id'=>$post['id']])->find())
            return json(['code'=>-1,'msg'=>'产品不存在']);
        if(!Db::name('product')->where(['id'=>$post['id']])->delete())
            return json(['code'=>-1,'msg'=>'删除失败']);
        $this->delFile('../uploads/'.$product['standard_file_url']);
        $this->delFile('../uploads/'.$product['source_file_url']);
        return json(['code'=>200,'msg'=>'删除成功']);
    }

    private function delFile($file_path){
        if (file_exists($file_path))
            unlink($file_path);
    }
}